package com.bojiu.lawsearch.lawsearch.util;

import com.bojiu.lawsearch.lawsearch.model.Log;
import com.bojiu.lawsearch.lawsearch.model.Task;
import com.bojiu.lawsearch.lawsearch.task.TaskInit;
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import com.tencentcloudapi.sms.v20190711.SmsClient;
import com.tencentcloudapi.sms.v20190711.models.SendSmsRequest;
import com.tencentcloudapi.sms.v20190711.models.SendSmsResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.util.Random;
import java.util.concurrent.TimeUnit;

/**
 * @author: afdiao
 * @Date：2020/3/29 0:47
 * @Description:
 */
@Component
@Slf4j
public class MsgUtil {

    public JsonResult sendMsg(String phone, String code) {
        Credential cred = new Credential("AKIDXw7CD517uRxPAzukTjDlHtZlsoeRk9pp", "75EIF4jfVYS6lEvrhTS3gWUZYAh6UsEN");

        HttpProfile httpProfile = new HttpProfile();
        httpProfile.setEndpoint("sms.tencentcloudapi.com");

        ClientProfile clientProfile = new ClientProfile();
        clientProfile.setHttpProfile(httpProfile);

        SmsClient client = new SmsClient(cred, "ap-shanghai", clientProfile);

        String params = "{\"PhoneNumberSet\":[\"+86" + phone + "\"],\"TemplateID\":\"563203\",\"Sign\":\"勤道传媒\",\"TemplateParamSet\":[\"" + code + "\"],\"SmsSdkAppid\":\"1400338638\"}";
        SendSmsRequest req = SendSmsRequest.fromJsonString(params, SendSmsRequest.class);
        try {
            SendSmsResponse resp = client.SendSms(req);
            String string = SendSmsRequest.toJsonString(resp);
            return new JsonResult(ResultCode.SUCCESS, string);
        } catch (TencentCloudSDKException e) {
            log.error("error to send msg,error is:{}", e);
            return new JsonResult(ResultCode.FAIL);
        }
    }

    public JsonResult sendMsgToUser(String phone) {
        Credential cred = new Credential("AKIDXw7CD517uRxPAzukTjDlHtZlsoeRk9pp", "75EIF4jfVYS6lEvrhTS3gWUZYAh6UsEN");

        HttpProfile httpProfile = new HttpProfile();
        httpProfile.setEndpoint("sms.tencentcloudapi.com");

        ClientProfile clientProfile = new ClientProfile();
        clientProfile.setHttpProfile(httpProfile);

        SmsClient client = new SmsClient(cred, "ap-shanghai", clientProfile);

        String params = "{\"PhoneNumberSet\":[\"+86" + phone + "\"],\"TemplateID\":\"681134\",\"Sign\":\"勤道传媒\",\"SmsSdkAppid\":\"1400338638\"}";
//        String params = "{\"PhoneNumberSet\":[\"+86" + phone + "\"],\"TemplateID\":\"681134\",\"Sign\":\"勤道传媒\",\"TemplateParamSet\":[\"" + code + "\"],\"SmsSdkAppid\":\"1400338638\"}";
        SendSmsRequest req = SendSmsRequest.fromJsonString(params, SendSmsRequest.class);
        try {
            SendSmsResponse resp = client.SendSms(req);
            String string = SendSmsRequest.toJsonString(resp);
            return new JsonResult(ResultCode.SUCCESS, string);
        } catch (TencentCloudSDKException e) {
            log.error("error to send msg,error is:{}", e);
            return new JsonResult(ResultCode.FAIL);
        }
    }

    public JsonResult sendMsgToEngineer(String phone) {
        Credential cred = new Credential("AKIDXw7CD517uRxPAzukTjDlHtZlsoeRk9pp", "75EIF4jfVYS6lEvrhTS3gWUZYAh6UsEN");

        HttpProfile httpProfile = new HttpProfile();
        httpProfile.setEndpoint("sms.tencentcloudapi.com");

        ClientProfile clientProfile = new ClientProfile();
        clientProfile.setHttpProfile(httpProfile);

        SmsClient client = new SmsClient(cred, "ap-shanghai", clientProfile);

        String params = "{\"PhoneNumberSet\":[\"+86" + phone + "\"],\"TemplateID\":\"681137\",\"Sign\":\"勤道传媒\",\"SmsSdkAppid\":\"1400338638\"}";
//        String params = "{\"PhoneNumberSet\":[\"+86" + phone + "\"],\"TemplateID\":\"563203\",\"Sign\":\"勤道传媒\",\"TemplateParamSet\":[\"" + code + "\"],\"SmsSdkAppid\":\"1400338638\"}";
        SendSmsRequest req = SendSmsRequest.fromJsonString(params, SendSmsRequest.class);
        try {
            SendSmsResponse resp = client.SendSms(req);
            String string = SendSmsRequest.toJsonString(resp);
            return new JsonResult(ResultCode.SUCCESS, string);
        } catch (TencentCloudSDKException e) {
            log.error("error to send msg,error is:{}", e);
            return new JsonResult(ResultCode.FAIL);
        }
    }

    public JsonResult sendMsgToUserForHandle(String phone) {
        Credential cred = new Credential("AKIDXw7CD517uRxPAzukTjDlHtZlsoeRk9pp", "75EIF4jfVYS6lEvrhTS3gWUZYAh6UsEN");

        HttpProfile httpProfile = new HttpProfile();
        httpProfile.setEndpoint("sms.tencentcloudapi.com");

        ClientProfile clientProfile = new ClientProfile();
        clientProfile.setHttpProfile(httpProfile);

        SmsClient client = new SmsClient(cred, "ap-shanghai", clientProfile);

        String params = "{\"PhoneNumberSet\":[\"+86" + phone + "\"],\"TemplateID\":\"681208\",\"Sign\":\"勤道传媒\",\"SmsSdkAppid\":\"1400338638\"}";
//        String params = "{\"PhoneNumberSet\":[\"+86" + phone + "\"],\"TemplateID\":\"563203\",\"Sign\":\"勤道传媒\",\"TemplateParamSet\":[\"" + code + "\"],\"SmsSdkAppid\":\"1400338638\"}";
        SendSmsRequest req = SendSmsRequest.fromJsonString(params, SendSmsRequest.class);
        try {
            SendSmsResponse resp = client.SendSms(req);
            String string = SendSmsRequest.toJsonString(resp);
            return new JsonResult(ResultCode.SUCCESS, string);
        } catch (TencentCloudSDKException e) {
            log.error("error to send msg,error is:{}", e);
            return new JsonResult(ResultCode.FAIL);
        }
    }

    /**
     * 生成手机验证码
     *
     * @param phone
     * @return
     */
    public String getPhoneCode(String phone) {
        String str = "0123456789";
        StringBuilder sb = new StringBuilder(4);
        for (int i = 0; i < 4; i++) {
            char ch = str.charAt(new Random().nextInt(str.length()));
            sb.append(ch);
        }
        TaskInit.phonecode.put(phone, sb.toString());
//        String phoneCodeKey = RedisUtil.getKeyBy3(RedisKey.PROJECT, RedisKey.PHONR_CODE, phone);
//        stringRedisTemplate.opsForSet().add(phoneCodeKey, sb.toString());
//        stringRedisTemplate.expire(phoneCodeKey, 20, TimeUnit.MINUTES);
        return sb.toString();
    }

    public boolean checkPhoneCode(String phone, String code) {
        if (TaskInit.phonecode.containsKey(phone)) {
            if (TaskInit.phonecode.get(phone).equals(code)) {
                return true;
            }
        }
//        String phoneCodeKey = RedisUtil.getKeyBy3(RedisKey.PROJECT, RedisKey.PHONR_CODE, phone);
//        if (stringRedisTemplate.hasKey(phoneCodeKey)) {
//            if (stringRedisTemplate.opsForSet().isMember(phoneCodeKey, code)) {
//                return true;
//            }
//        }
        return false;

    }
}
